package com.slwish.xty.mvp.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.slwish.common.mvp.presenter.BasePresenter;
import com.slwish.common.utils.LogUtils;
import com.slwish.xty.R;
import com.slwish.xty.config.UrlConfig;
import com.slwish.xty.entity.CreditTourismInfoModel;
import com.slwish.xty.entity.MainBannerModel;
import com.slwish.xty.mvp.contract.MainContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.litepal.util.LogUtil;

import java.util.List;


/**
 * 首页
 *
 * @author 陈浩
 * @version 1.0.0
 */
public class MainPresenter extends BasePresenter<MainContract.View>
        implements MainContract.Presenter {

    public MainPresenter(MainContract.View view) {
        // 初始化数据仓库
        super(view);
    }


    @Override
    public void start() {
        super.start();

        // 加载网络数据
//        UserHelper.refreshContacts();

    }
//
//    // 运行到这里的时候是子线程
//    @Override
//    public void onDataLoaded(List<MainModel.DataBean> mainModels) {
//        // 无论怎么操作，数据变更，最终都会通知到这里来
//        final MainContract.View view = getView();
//        if (view == null)
//            return;
//        BaseQuickAdapter<MainModel.DataBean, BaseViewHolder> adapter = view.getRecyclerAdapter();
//        List<MainModel.DataBean> old = adapter.getData();
//
//        // 进行数据对比
////        DiffUtil.Callback callback = new DiffUiDataCallback<MainModel>(old, mainModels);
////        DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
////文艺青年新宠
////利用DiffUtil.calculateDiff()方法，传入一个规则DiffUtil.Callback对象，和是否检测移动item的 boolean变量，得到DiffUtil.DiffResult 的对象
//        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(old, mainModels), true);
////利用DiffUtil.DiffResult对象的dispatchUpdatesTo（）方法，传入RecyclerView的Adapter，轻松成为文艺青年
//
//        diffResult.dispatchUpdatesTo(adapter);
//        // 调用基类方法进行界面刷新
//        refreshData(diffResult, mainModels);
//    }

    @Override
    public void getBannerData() {

        EasyHttp.get(UrlConfig.QUERY_BANNER_URL).cacheKey(UrlConfig.QUERY_BANNER_URL)
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String json) {
                        LogUtil.d("xx", "mainBannerModel:" + json.toString());
                        Gson gson = new Gson();
                        MainBannerModel model = gson.fromJson(json, MainBannerModel.class);
                        if (model != null && model.getResultCode().equals("0")) {
                            getView().getBannerDataSuccess(model);
                        }
                    }
                });
    }

    @Override
    public void getMainListData(String longitude, String latitude, int page) {
        EasyHttp.post(UrlConfig.SEARCH_MERCHANT_BY_SITE_URL)
                .cacheKey(UrlConfig.SEARCH_MERCHANT_BY_SITE_URL)
                .params("longitude", longitude)
                .params("latitude", latitude)
                .params("page", page + "")
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }
                    @Override
                    public void onSuccess(String json) {
                        LogUtils.i("xx","json:"+json);

                        try {
                            JSONObject jsonObject = JSON.parseObject(json);
                            String resultCode = (String) jsonObject.get("resultCode");
                            String resultMessage = (String) jsonObject.get("resultMessage");
                            String data = jsonObject.getString("data");
                            //return
                            if (!TextUtils.equals(resultCode, "0")) {
                                setError(resultMessage);
                                return;
                            }
                            List<CreditTourismInfoModel> list = JSON.parseArray(data, CreditTourismInfoModel.class);
                            if (list != null && getView() != null) {
                                getView().getMainListDataSuccess(list);
//                                setError(R.string.login_success_text);
                            }
                        } catch (Exception e) {
                            setError(R.string.loadig_failed_text);
                        }

                    }
                });
    }
}
